package alsTelecom.Http;

import java.util.Vector;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransport;
import alsTelecom.Device.Excepciones;
import alsTelecom.Objects.Lista;
import alsTelecom.Objects.Noticia;
import alsTelecom.Objects.Utils;

public class ConexionN24 
{
	/**
	 * 
	 * @param categ
	 * @param pag
	 * @return
	 */
	public static Lista[] GetLista(int categ,int pag){
		try{
			
			String serviceUrl = "http://blackberry.noticias24.com/server.php?wsdl"+ConnectionString.getConnectionString();
			String serviceNamespace = "http://blackberry.noticias24.com/"+ConnectionString.getConnectionString();
			String soapAction = "http://blackberry.noticias24.com/getLista"+ConnectionString.getConnectionString();
			String methodName = "getLista";
			SoapObject rpc = new SoapObject(serviceNamespace, methodName);
			rpc.addProperty("isbn", ""+categ);
			rpc.addProperty("pag", ""+pag);
			SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);

			envelope.bodyOut = rpc;
			envelope.dotNet = true;
			envelope.encodingStyle = SoapSerializationEnvelope.ENC;

			HttpTransport ht = new HttpTransport(serviceUrl);
			ht.debug = true;
			ht.setXmlVersionTag("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); 

			ht.call(soapAction, envelope);
			if(envelope.getResponse() !=null){
			Vector result = new Vector();
			result = (Vector) envelope.getResponse();
			
			return Utils.RetrieveListaFromSoap(result);
			}
			return null;
		}
        catch(Exception ex){
        	 String mensaje = "Error Descargando Lista (categ: "+categ+",pag: "+pag+")";
	    	 Excepciones.notifyException(3,mensaje,ex);
        	return null;
        }
	}
	
	/**
	 * 
	 * @param categ
	 * @param id
	 * @return
	 */
	public static Noticia GetNoticia(int categ,int id) throws Exception{
		
			
			String serviceUrl = "http://blackberry.noticias24.com/server.php?wsdl"+ConnectionString.getConnectionString();
			String serviceNamespace = "http://blackberry.noticias24.com/"+ConnectionString.getConnectionString();
			String soapAction = "http://blackberry.noticias24.com/getNoticia"+ConnectionString.getConnectionString();
			String methodName = "getNoticia";
			SoapObject rpc = new SoapObject(serviceNamespace, methodName);
			rpc.addProperty("isbn", ""+categ);
			rpc.addProperty("id", ""+id);
			SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);

			envelope.bodyOut = rpc;
			envelope.dotNet = true;
			envelope.encodingStyle = SoapSerializationEnvelope.ENC;

			HttpTransport ht = new HttpTransport(serviceUrl);
			ht.debug = true;
			ht.setXmlVersionTag("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); 

			ht.call(soapAction, envelope);
			if(envelope.getResponse() !=null){
			Vector result = new Vector();
			result = (Vector) envelope.getResponse();
			return Utils.RetrieveNoticiaFromSoap(result);
			}
			return null;
		
	}
	
}



